package com.mytinyRPC.framework.loadBalance.Impl;



import com.mytinyRPC.Factory.SingletonFactory;
import com.mytinyRPC.framework.URL;
import com.mytinyRPC.framework.loadBalance.LoadBalance;
import com.mytinyRPC.register.ServiceDiscovery;
import com.mytinyRPC.register.zookeeperRegister.ZkServiceDiscovery;

import java.security.Provider;
import java.util.List;
import java.util.Random;

/***
 * @Author HP
 * @Description //TODO 随机负载均衡
 **/
public class RandomLoadBalance implements LoadBalance {


    private static final ServiceDiscovery serviceDiscovery;
    static {
        serviceDiscovery = SingletonFactory.getInstance(ZkServiceDiscovery.class);
    }
    private URL random(List<URL> list){
        Random random = new Random();
        int i = random.nextInt(list.size());
        System.out.println("选择负载服务器:"+ list.get(i).toString());
        return list.get(i);
    }

    public URL loadBalance(String interfaceName) {
        List<URL> urlList= serviceDiscovery.getURL(interfaceName);
        return random(urlList);
    }
}
